package com.wang.interview;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.wang.spring.Role;
import com.wang.spring.UserInfo;

public class MybatisTest {
	
	private static SqlSessionFactory sqlSessionFactory;
	static{
		 try {
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");			
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {		
			e.printStackTrace();
		}
	}
	
	public static SqlSessionFactory getSessionFactory() {
		return sqlSessionFactory;
	}

	
	
	public static void main(String[] args) {
		SqlSession session = sqlSessionFactory.openSession();
		try{
			//一对多测试
//			UserInfo userInfo = session.selectOne("com.wang.spring.UserInfoMapper.getUser", "demo02");
//			System.out.println(userInfo);
			
//			多对一测试：
			Role role = session.selectOne("com.wang.spring.UserInfoMapper.getUserRoles", 123);
			System.out.println(role);
						
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			session.close();
		}		
	}
	
	
	
	public static void testDelete(){
		 SqlSession session = sqlSessionFactory.openSession();
         IUser iUser = session.getMapper(IUser.class);
         iUser.deleteUser("demo01");
         session.commit();
         
	}
}
